package com.book.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.book.bean.Users;
import com.book.biz.UserBiz;
import com.book.biz.impl.UserBizImpl;



public class RegisterServelet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		
		String uname =request.getParameter("uname");
		String password = request.getParameter("password");
		String email = request.getParameter("email");
		String IDnumber = request.getParameter("IDnumber");
		String phone = request.getParameter("phone");
		String postcode = request.getParameter("postcode");
		String address = request.getParameter("address");
		
		Users user = new Users();
		user.setUname(uname);
		user.setPassword(password);
		user.setEmail(email);
		user.setIDnumber(IDnumber);
		user.setPhone(phone);
		user.setPostcode(postcode);
		user.setAddress(address);
		
		UserBiz userBiz = new UserBizImpl();
		boolean flag = userBiz.register(user);
		
		if(flag){
			out.print("<script>alert('OK');location='login_user.jsp';</script>");
		}else{
			request.getRequestDispatcher("/error.jsp").forward(request, response);
		}
		
	}

}
